﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAO;
using System.Data.SqlClient;
using System.Data;


namespace APA_DAO
{
    public class ConfigDao
    {
        private string PASS_PHASER = "AMS";
        public ConfigDao()
        {
            new SqlHelper();
        }

        public DataRow getConfig()
        {
            DataTable table = SqlHelper.ExecuteDataTable("SP_GET_CONFIG", CommandType.StoredProcedure);
            if (table != null && table.Rows.Count > 0)
            {
                return table.Rows[0];
            }
            return null;
        }

        public bool saveConfig(string electPrice, string waterPrice,string motoPrice, string email, string pass)
        {
            try
            {
                SqlTransaction sqlTransaction;
                SqlConnection con = new SqlConnection(SqlHelper.ConnectString);
                con.Open();
                sqlTransaction = con.BeginTransaction(IsolationLevel.ReadCommitted);
                try
                {
                    if (!SqlHelper.ExecuteNonQueryWithTranSaction("SP_SAVE_CONFIG", sqlTransaction, con,
                        "@NAME", "ELE_PRICE",
                        "@VAL", electPrice,
                        "@USR_ID", LoginDao.userID))
                    {
                        sqlTransaction.Rollback();
                        return false;
                    }

                    if (!SqlHelper.ExecuteNonQueryWithTranSaction("SP_SAVE_CONFIG", sqlTransaction, con,
                        "@NAME", "EMAIL",
                        "@VAL", email,
                        "@USR_ID", LoginDao.userID))
                    {
                        sqlTransaction.Rollback();
                        return false;
                    }
                    if (!SqlHelper.ExecuteNonQueryWithTranSaction("SP_SAVE_CONFIG", sqlTransaction, con,
                        "@NAME", "WAT_PRICE",
                        "@VAL", waterPrice,
                        "@USR_ID", LoginDao.userID))
                    {
                        sqlTransaction.Rollback();
                        return false;
                    }


                    if (!SqlHelper.ExecuteNonQueryWithTranSaction("SP_SAVE_CONFIG", sqlTransaction, con,
                       "@NAME", "MOTO_PRICE",
                       "@VAL", motoPrice,
                       "@USR_ID", LoginDao.userID))
                    {
                        sqlTransaction.Rollback();
                        return false;
                    }

                    if (!SqlHelper.ExecuteNonQueryWithTranSaction("SP_SAVE_CONFIG", sqlTransaction, con,
                       "@NAME", "PASS",
                       "@VAL", SimpleEnrypt.Encrypt(pass,this.PASS_PHASER),
                       "@USR_ID", LoginDao.userID))
                    {
                        sqlTransaction.Rollback();
                        return false;
                    }
                    sqlTransaction.Commit();
                    return true;
                }
                catch (Exception e)
                {
                    sqlTransaction.Rollback();
                    Console.WriteLine(e);
                    return false;
                }
                finally
                {
                    con.Close();
                    con.Dispose();
                }
            }
            catch (Exception ex)
            {
                Console.Write(ex.ToString());
                return false;
            }
        }
    }
}
